library(rio)
library(tidyverse)
library(ggstance)
library(cowplot)
library(ggpubr)

#To help in calling the .dta files
path_in = './Data/'


###########################
#Predicted Probability
###########################
##Load Data
program_probs <- import(paste(path_in, 'program_probs.dta', sep='')) %>%
  mutate(outcome = "Programmatic")

ideol_probs <- import(paste(path_in, 'ideol_probs.dta', sep='')) %>%
  mutate(outcome = "Ideology")

princ_probs <- import(paste(path_in, 'princ_probs.dta', sep='')) %>%
  mutate(outcome = "Principles")

policy_probs <- import(paste(path_in, 'policy_probs.dta', sep='')) %>%
  mutate(outcome = "Policy")

sgroups_probs <- import(paste(path_in, 'sgroups_probs.dta', sep='')) %>%
  mutate(outcome = "S. Groups")

process_probs <- import(paste(path_in, 'process_probs.dta', sep='')) %>%
  mutate(outcome = "Process")

traits_probs <- import(paste(path_in, 'traits_probs.dta', sep='')) %>%
  mutate(outcome = "Traits")

polit_probs <- import(paste(path_in, 'polit_probs.dta', sep='')) %>%
  mutate(outcome = "Politicians")

probs_comb <- bind_rows(program_probs, ideol_probs, princ_probs, policy_probs, 
                      sgroups_probs, polit_probs, traits_probs, process_probs ) %>%
  mutate(labs = case_when(
    outcome == "Programmatic" ~ "b = -2.17, p < 0.001", 
    outcome == "Ideology" ~ "b = 2.93, p <0.001", 
    outcome == "Principles" ~ "b = 1.83, p < 0.001", 
    outcome == "Policy" ~ "b = 1.48, p < 0.001", 
    outcome == "S. Groups" ~ "b = 0.99, p < 0.01", 
    outcome == "Politicians" ~ "b = 0.50, p = 0.16", 
    outcome == "Traits" ~ "b = 0.29, p = 0.52", 
    outcome == "Process" ~ "b = 0.96, p < 0.01"))

#####Figure
probs_comb1 <- probs_comb %>%
  mutate(outcome = factor(outcome, 
                          levels=c("Programmatic", "Ideology", 
                                   "Principles", "Policy", 
                                   "S. Groups", "Politicians", 
                                   "Traits", "Process")))
probs_comb1 %>%
  mutate(sig = case_when(
    outcome %in% c("Programmatic", "Ideology", "Principles", "Policy", 
                   "S. Groups", "Process") ~ "x", 
    outcome %in% c("Politicians", "Traits") ~ "y")) %>%
  ggplot(aes(x=`_at3`, y=`_margin`, shape = sig)) + 
  geom_pointrange(aes(ymin=`_ci_lb`, ymax=`_ci_ub`)) +
  geom_line() + 
  theme_light(14) +
  facet_wrap(. ~ outcome, nrow=2) +
  theme(strip.text.x = element_text(face="bold", 
                                    color="black"), 
        legend.position = "none") + 
  labs(y = "Predicted Probability\n", 
       x = "Knowledge") + 
  scale_shape_manual(values=c(16, 1))


ggsave(paste(path_in, "figure7.png", sep=''), 
       height=8, width=14, dpi=1200)


export(probs_comb1, paste(path_in, "probs.csv", sep=''))
